package com.shanshan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shanshan.system.entity.News;
import com.shanshan.system.entity.NewsKind;
import com.shanshan.system.mapper.NewsKindMapper;
import com.shanshan.system.mapper.NewsMapper;
import com.shanshan.system.service.INewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanshan.system.util.JwtUtil;
import com.shanshan.system.util.WebUtil;
import com.shanshan.system.vo.Result;
import com.shanshan.system.vo.SearchContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shanshan
 * @since 2023-01-31
 */
@Service
@SuppressWarnings("all")
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsKindMapper newsKindMapper;
    @Override
    public Result addNews(News news) {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        String username = (String) map.get("username");
        news.setAuthor(username);
        int insert = newsMapper.insert(news);
        if (insert==1){
            return new Result(200,"添加成功，请去查询。");
        }
        return new Result(500,"添加失败");
    }

    @Override
    public Result selectAll(int currentPage, int pageSize, SearchContent searchContent) {
        QueryWrapper<News> wrapper = new QueryWrapper<>();
        if (searchContent.getAuthor()!=null){
            wrapper.like("author",searchContent.getAuthor());
        }
        if (searchContent.getTitle()!=null){
            wrapper.like("title",searchContent.getTitle());
        }
        if (searchContent.getTime()!=null){
            searchContent.setBeginDate(searchContent.getTime()[0]);
            searchContent.setEndDate(searchContent.getTime()[1]);
            wrapper.between("time",searchContent.getBeginDate(),searchContent.getEndDate());
        }

        if (searchContent.getKid()!=0){
            wrapper.like("kid",searchContent.getKid());
        }

        PageHelper.startPage(currentPage,pageSize);
        List<News> list = newsMapper.selectList(wrapper);
        for (News news : list) {
            Integer kid = news.getKid();
            NewsKind newsKind = newsKindMapper.selectById(kid);
            news.setKind(newsKind.getKind());
        }

        PageHelper.startPage(currentPage,pageSize);

        PageInfo<News> info = new PageInfo<>(list);

        return new Result(200,"查询成功",info);
    }

    @Override
    public Result refuse(News news) {
        news.setStatus(3);
        int i = newsMapper.updateById(news);
        return new Result(200,"完成");
    }

    @Override
    public Result agree(Integer id) {

        newsMapper.updateStatus(id,1);
        return new Result(200,"完成");
    }

    /**
     * @param id:
     * @param status:
     * @return Result
     * @description 修改新闻的上下架
     */
    @Override
    public Result upUpdate(int id,int status) {
        if (status==1||status==4){
            status=2;
        }
        else{
            status=4;
        }

        newsMapper.updateStatus(id,status);
        return new Result(200,"修改成功");
    }

    /**
     * @param id:
     * @param uppid:
     * @return Result
     * @description 修改新闻的置顶和取消置顶
     */
    @Override
    public Result topUpdate(int id,int uppid) {
        if (uppid==5){
            uppid=6;
        }
        else {
            uppid=5;
        }
        newsMapper.updateUppid(id,uppid);
        return new Result(200,"修改成功");
    }

    @Override
    public Result findAllKind1() {
        List<News> allKind1 = newsMapper.findAllKind1();
        return new Result(200,"查询新闻",allKind1);
    }

    @Override
    public Result findAllKind2() {
        List<News> allKind1 = newsMapper.findAllKind2();
        return new Result(200,"查询新闻",allKind1);
    }

    @Override
    public Result findAllKind1ById(Integer id) {
        News allKind1ById = newsMapper.findAllKind1ById(id);
        return new Result(200,"查询成功",allKind1ById);
    }

    @Override
    public Result findAllKind2ById(Integer id) {
        News allKind1ById = newsMapper.findAllKind2ById(id);
        return new Result(200,"查询成功",allKind1ById);
    }
}
